package promote.brozen.netty.echo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author Brozen
 * @date 2020/9/25 4:40 PM
 * @email brozen@qq.com
 */
public class NettyEchoServer {

    public void start() throws InterruptedException {
        /*
            两个线程组，用于网络事件处理，就是Reactor线程组。
            bossGroup用于接收网络请求。
            workGroup用于客户端网络请求的读写。

            这个demo没有考虑拆包粘包
         */
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap server = new ServerBootstrap();
            server.group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childHandler(new NettyEchoChildChannelHandler());

            ChannelFuture bindFuture = server.bind(8090).sync();

            System.out.println("服务启动成功！");
            bindFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }

    }

    public static void main(String[] args) throws InterruptedException {
        new NettyEchoServer().start();
    }

}
